﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;

namespace Ymatou.File.Lib
{
    public class FileHelper
    {
        public static string PhysicalBasePath
        {
            get
            {
                //获取上传根路径
                string basePath = ConfigurationManager.AppSettings["UploadPath"];

                if (string.IsNullOrEmpty(basePath))
                {
                    //使用默认路径
                    basePath = "uploads";
                }

                if (!Path.IsPathRooted(basePath))
                {
                    //根路径是相对路径，则按照当前web项目的路径存储
                    basePath = Path.Combine(HttpContext.Current.Server.MapPath("/"), basePath);
                }
                return basePath;
            }
        }

        public static string ValidateKey
        {
            get
            {
                string validateKey = ConfigurationManager.AppSettings["ValidateKey"];

                if (string.IsNullOrEmpty(validateKey))
                {
                    //默认的key
                    validateKey = "bh051986943966";
                }

                return validateKey;
            }
        }

        public static string FileAccessDomain
        {
            get
            {
                string domain = ConfigurationManager.AppSettings["Domain"];

                if (string.IsNullOrEmpty(domain))
                {
                    domain = HttpContext.Current.Request.ServerVariables["http_host"];
                    domain = "http://" + domain;

                    //string serverPort = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
                    //if(serverPort != "80")
                    //{
                    //    domain += ":" + serverPort;
                    //}
                }

                return domain;
            }
        }

        /// <summary>
        /// 下载文件时使用的路由
        /// </summary>
        public static string AccessRoute
        {
            get
            {
                string accessRoute = ConfigurationManager.AppSettings["AccessRoute"];

                if (string.IsNullOrEmpty(accessRoute))
                {
                    accessRoute = "files";
                }

                return accessRoute;
            }
        }

        public static string WaterMarkFolder
        {
            get
            {
                string waterMarkFolder = ConfigurationManager.AppSettings["watermark"];
                if (string.IsNullOrEmpty(waterMarkFolder))
                {
                    waterMarkFolder = "watermark";
                }

                return waterMarkFolder;
            }
        }

        public static string GetContentType(string fileExt)
        {
            string contentType = "application/octet-stream";

            if(!string.IsNullOrEmpty(fileExt))
            {
                fileExt = fileExt.ToLower();

                switch (fileExt)
                {
                    case "pdf":
                        contentType = "application/pdf";
                        break;
                    case "jpg":
                    case "jpeg":
                        contentType = "image/jpeg";
                        break;
                    case "png":
                        contentType = "image/png";
                        break;
                    case "gif":
                        contentType = "image/gif";
                        break;
                    case "bmp":
                        contentType = "image/bmp";
                        break;
                    case "zip":
                        contentType = "application/zip";
                        break;
                    default:
                        contentType = "application/octet-stream";
                        break;
                }
            }

            return contentType;
        }
    }
}